package com.rock.code.report.monitor.controller;

import com.rock.code.base.controller.BaseController;
import com.rock.code.report.monitor.service.JbMonitorReportService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
* 监控
*
* @author xubh
* @date 2018/01/21
*/
@Controller
@RequestMapping("/MonitorReport")
public class JbMonitorReportController extends BaseController {

    /**
    * The jbMonitorReportService
    */
    @Resource
    private JbMonitorReportService jbMonitorReportService;


    @RequestMapping("")
    public String monitorPage(){

        return "monitor/monitorView";
    }


    /**
     * alarmTendencyContrast 最近24小时严重告警趋势对比
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    /*@ResponseBody
    @RequestMapping(value = "/alarmTendencyContrast", method = RequestMethod.GET)
    public HashMap alarmTendencyContrast() throws Exception {
        HashMap resultMap=new HashMap();
        List resultList=new ArrayList();
        try{
            Calendar cal = Calendar.getInstance ();
            Calendar cal1 = Calendar.getInstance ();
            Calendar cal2 = Calendar.getInstance ();
            for(int i=1;i<=24;i++){
                if(i==1){
                    SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                    String drtime=dateFormat1.format(cal.getTime());
                    cal1.set(Calendar.DATE, cal1.get(Calendar.DATE) - 1);
                    String hbtime=dateFormat1.format(cal1.getTime());
                    cal2.set(Calendar.MONTH, cal2.get(Calendar.MONTH) - 1);
                    String tbtime=dateFormat1.format(cal2.getTime());
                    System.out.println(drtime+"=========="+hbtime+"========="+tbtime);
                    HashMap map=new HashMap();
                    map.put("drtime",drtime);
                    map.put("tbtime",tbtime);
                    map.put("hbtime",hbtime);
                    map.put("hour",drtime.substring(11));
                    List list=jbMonitorReportService.queryAlarmTendencyContrast(map);
                    HashMap hourMap=(HashMap) list.get(0);
                    resultList.add(hourMap);
                }else {
                    cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
                    SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                    String drtime = dateFormat1.format(cal.getTime());
                    Date drdate=dateFormat1.parse(drtime);
                    cal1.setTime(drdate);
                    cal1.set(Calendar.DATE, cal1.get(Calendar.DATE) - 1);
                    String hbtime=dateFormat1.format(cal1.getTime());
                    cal2.setTime(drdate);
                    cal2.set(Calendar.MONTH, cal2.get(Calendar.MONTH) - 1);
                    String tbtime=dateFormat1.format(cal2.getTime());
                    System.out.println(drtime+"=========="+hbtime+"========="+tbtime);
                    HashMap map=new HashMap();
                    map.put("drtime",drtime);
                    map.put("tbtime",tbtime);
                    map.put("hbtime",hbtime);
                    map.put("hour",drtime.substring(11));
                    List list=jbMonitorReportService.queryAlarmTendencyContrast(map);
                    HashMap hourMap=(HashMap) list.get(0);
                    resultList.add(hourMap);
                }
            }
            resultMap.put("resultList",resultList);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }*/
    @ResponseBody
    @RequestMapping(value = "/alarmTendencyContrast", method = RequestMethod.POST)
    public HashMap alarmTendencyContrast() throws Exception {
        HashMap resultMap=new HashMap();
        List packList=new ArrayList();
        List list=new ArrayList();
        String dr_start_time="";
        String tb_start_time="";
        String hb_start_time="";
        String dr_end_time="";
        String tb_end_time="";
        String hb_end_time="";
        try{
            Calendar cal = Calendar.getInstance ();
            Calendar cal1 = Calendar.getInstance ();
            Calendar cal2 = Calendar.getInstance ();
            for(int i=1;i<=24;i++){
                if(i==1){
                    SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                    String drtime=dateFormat1.format(cal.getTime());
                    cal1.set(Calendar.DATE, cal1.get(Calendar.DATE) - 1);
                    String hbtime=dateFormat1.format(cal1.getTime());
                    cal2.set(Calendar.MONTH, cal2.get(Calendar.MONTH) - 1);
                    String tbtime=dateFormat1.format(cal2.getTime());
                    packList.add(drtime.substring(11));
                    dr_end_time=drtime+":59:59";
                    tb_end_time=tbtime+":59:59";
                    hb_end_time=hbtime+":59:59";

                }else {
                    cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
                    SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                    String drtime = dateFormat1.format(cal.getTime());
                    packList.add(drtime.substring(11));
                    if(i==24){
                        Date drdate=dateFormat1.parse(drtime);
                        cal1.setTime(drdate);
                        cal1.set(Calendar.DATE, cal1.get(Calendar.DATE) - 1);
                        String hbtime=dateFormat1.format(cal1.getTime());
                        cal2.setTime(drdate);
                        cal2.set(Calendar.MONTH, cal2.get(Calendar.MONTH) - 1);
                        String tbtime=dateFormat1.format(cal2.getTime());
                        dr_start_time=drtime+":00:00";
                        tb_start_time=tbtime+":00:00";
                        hb_start_time=hbtime+":00:00";
                    }
                }
            }
            List drList=jbMonitorReportService.querySeverityAlarmList(dr_start_time,dr_end_time);
            List tbList=jbMonitorReportService.querySeverityAlarmList(tb_start_time,tb_end_time);
            List hbList=jbMonitorReportService.querySeverityAlarmList(hb_start_time,hb_end_time);
            HashMap drMap=new HashMap();
            HashMap tbMap=new HashMap();
            HashMap hbMap=new HashMap();
            if(drList!=null&&drList.size()>0){
                for(int a=0;a<drList.size();a++){
                    HashMap drmap=(HashMap)drList.get(a);
                    String time=drmap.get("GENERANT_TIME").toString();
                    String gj_count=drmap.get("GJ_COUNT").toString();
                    drMap.put(time.substring(11),gj_count);
                }
            }
            if(tbList!=null&&tbList.size()>0){
                for(int b=0;b<tbList.size();b++){
                    HashMap tbmap=(HashMap)tbList.get(b);
                    String time=tbmap.get("GENERANT_TIME").toString();
                    String gj_count=tbmap.get("GJ_COUNT").toString();
                    tbMap.put(time.substring(11),gj_count);
                }
            }
            if(hbList!=null&&hbList.size()>0){
                for(int c=0;c<hbList.size();c++){
                    HashMap hbmap=(HashMap)hbList.get(c);
                    String time=hbmap.get("GENERANT_TIME").toString();
                    String gj_count=hbmap.get("GJ_COUNT").toString();
                    hbMap.put(time.substring(11),gj_count);
                }
            }
            for(int j=packList.size()-1;j>=0;j--){
                String hour=packList.get(j).toString();
                String dr_count = drMap.get(hour)==null?"0":drMap.get(hour).toString();
                String tb_count = tbMap.get(hour)==null?"0":tbMap.get(hour).toString();
                String hb_count = hbMap.get(hour)==null?"0":hbMap.get(hour).toString();
                HashMap map=new HashMap();
                map.put("hour",hour);
                map.put("dr_count",dr_count);
                map.put("tb_count",tb_count);
                map.put("hb_count",hb_count);
                list.add(map);
            }
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryTodayAlarmCount 当日监控告警对比
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryTodayAlarmCount", method = RequestMethod.POST)
    public HashMap queryTodayAlarmCount() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbMonitorReportService.queryTodayAlarmCount();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryTodayAlarmLevel 当日告警级别占比
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryTodayAlarmLevel", method = RequestMethod.POST)
    public HashMap queryTodayAlarmLevel() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbMonitorReportService.queryTodayAlarmLevel();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryMonitorKpiCount 监控指标数量视图
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryMonitorKpiCount", method = RequestMethod.POST)
    public HashMap queryMonitorKpiCount() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbMonitorReportService.queryMonitorKpiCount();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryPlatformAngle 平台角度视图
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryPlatformAngle", method = RequestMethod.POST)
    public HashMap queryPlatformAngle() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbMonitorReportService.queryPlatformAngle();
            List counts=jbMonitorReportService.queryMonitorKpiCount();
            resultMap.put("list",list);
            resultMap.put("counts",counts);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryPlatformAlarmSituation 平台告警处理情况视图
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryPlatformAlarmSituation", method = RequestMethod.POST)
    public HashMap queryPlatformAlarmSituation() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbMonitorReportService.queryPlatformAlarmSituation();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryBusinessAngle 业务角度视图
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryBusinessAngle", method = RequestMethod.POST)
    public HashMap queryBusinessAngle() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbMonitorReportService.queryBusinessAngle();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    /**
     * queryBusinessAlarmSituation 业务告警处理情况视图
     *
     * @return TbSysDepartment
     * @throws Exception the exception
     * @author xubh
     * @date 2018/01/06
     */
    @ResponseBody
    @RequestMapping(value = "/queryBusinessAlarmSituation", method = RequestMethod.POST)
    public HashMap queryBusinessAlarmSituation() throws Exception {
        HashMap resultMap=new HashMap();
        try{
            List list=jbMonitorReportService.queryBusinessAlarmSituation();
            resultMap.put("list",list);
        }catch (Exception e){
            e.printStackTrace();
        }
        return resultMap;
    }

    public static void main(String args[]) throws Exception {
        Calendar cal = Calendar.getInstance ();
        Calendar cal1 = Calendar.getInstance ();
        Calendar cal2 = Calendar.getInstance ();
        for(int i=1;i<=24;i++){
            if(i==1){
                SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                String drtime=dateFormat1.format(cal.getTime());
                cal1.set(Calendar.DATE, cal1.get(Calendar.DATE) - 1);
                String hbtime=dateFormat1.format(cal1.getTime());
                cal2.set(Calendar.MONTH, cal2.get(Calendar.MONTH) - 1);
                String tbtime=dateFormat1.format(cal2.getTime());
                System.out.println(drtime+"=========="+hbtime+"========="+tbtime);
                System.out.println(drtime.substring(11));
            }else {
                cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) - 1);
                SimpleDateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd HH");
                String drtime = dateFormat1.format(cal.getTime());
                Date drdate=dateFormat1.parse(drtime);
                cal1.setTime(drdate);
                cal1.set(Calendar.DATE, cal1.get(Calendar.DATE) - 1);
                String hbtime=dateFormat1.format(cal1.getTime());
                cal2.setTime(drdate);
                cal2.set(Calendar.MONTH, cal2.get(Calendar.MONTH) - 1);
                String tbtime=dateFormat1.format(cal2.getTime());
                System.out.println(drtime+"=========="+hbtime+"========="+tbtime);
                System.out.println(drtime.substring(11));
            }
        }
    }

}
